package cn.hinglo.gameclaw.client.action;

import cn.hinglo.gameclaw.client.service.GrabNoteService;
import cn.hinglo.gameclaw.data.controller.BaseController;
import cn.hinglo.gameclaw.data.entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @createTime: 2018/1/17
 * @author: HingLo
 * @description: 抓取记录
 */
@RestController
@RequestMapping("/client/g/")
public class GrabNoteAction extends BaseController {

    private final GrabNoteService grabNoteService;

    @Autowired
    public GrabNoteAction(GrabNoteService grabNoteService) {
        this.grabNoteService = grabNoteService;
    }

    /**
     * @param status 抓取记录状态（抓中，未抓中)
     * @return 返回列表
     */
    @GetMapping("list")
    public Result listGrabNote(boolean status) {
        Integer page = 1;
        String userId = getUserId(request);
        //客户端默认加载10条
        Map<String, Object> map = grabNoteService.listGrabNoteService(userId, status, page, 10);
        return success(map.get("result"));
    }

    /**
     * 娃娃机入队申请
     *
     * @param cid 娃娃机的编号
     * @return 入队结果
     */
    @PostMapping("queue")
    public Result enqueue(int cid) {
        String userId = getUserId(request);
        return grabNoteService.lineUPService(cid, userId);
    }

    /**
     * pc 端的抓取结果通知
     *
     * @param id     娃娃机编号
     * @param status 是否抓中，true：抓中 false 没有抓中
     * @return 返回结果
     */
    @PostMapping("result")
    public Result result(Integer id, Boolean status) {
        if (id < 1 || status == null) {
            return error("通知结果参数不正确");
        }
        return grabNoteService.resultService(id, status);
    }

    /**
     * 排队抢的检测
     *
     * @param cid 娃娃机Id
     * @return 返回结果
     */
    @PostMapping("check")
    public Result check(int cid) {
        return success(grabNoteService.checkService(cid, getUserId(request)));

    }
}
